import axios from "axios";
import Vue from 'vue'
import router from "../router/index";
import { config } from "./params";

let $post = (url, data) => {
  console.log(data)
  const token = localStorage.getItem("token");
  return new Promise((resolve, reject) => {
    axios({
      method: "POST",
      url: config.requestUrl + url,
      headers:{
        'Content-Type': 'application/json',
        Authorization: token,
      },
      data
    })
      .then((res) => {
        if (res.data.status === 402) {
          router.push("/login");
          ElNotification({
            title: "Error",
            offset: 100,
            message: "身份验证失败，请重新登录！",
            type: "error",
          });
        } else resolve(res.data);
      })
      .catch((err) => {
        console.log(err);
        reject(err);
      });
  });
};

export default {
  install: (Vue) => {
    // Vue 2.x 使用 Vue.prototype 来挂载全局方法
    Vue.prototype.$post = $post;
  },
};
